package tags;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import groovy.lang.Closure;
import play.templates.FastTags;
import play.templates.GroovyTemplate.ExecutableTemplate;
import play.templates.JavaExtensions;

/**
 * 自定义标签
 * 
 * @ClassName: AuthorityTag 
 * @author: wangsm
 * @date: 2016年5月25日 上午10:51:53
 */
@FastTags.Namespace("my.tags") 
public class AuthorityTag extends FastTags {
	private static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat datetimeFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 自定义权限标签
	 *
	 * @Title: _authority 
	 * @param args
	 * @param body
	 * @param out
	 * @param template
	 * @param fromLine
	 * @return: void
	 */
	public static void _authority(Map<?, ?> args, Closure body, PrintWriter out, 
			   ExecutableTemplate template, int fromLine) {
			   String Auth=(String)(args.get("Auth"));
			   if(StringUtils.isNotEmpty(Auth)){
				   out.println(JavaExtensions.toString(body));
			   }
	}
	/**
	 * 自定义格式化日期标签
	 *
	 * @Title: _authority 
	 * @param args
	 * @param body
	 * @param out
	 * @param template
	 * @param fromLine
	 * @return: void
	 */
	public static void _formatedate(Map<?, ?> args, Closure body, PrintWriter out, 
			   ExecutableTemplate template, int fromLine) {
			   Date date=(Date)(args.get("date"));
			   String type=(String)(args.get("type"));
			   if(date==null||StringUtils.isEmpty(type)){
				   out.println("");
				   return;
			   }
			   if("datetime".equals(type)){
				   out.println(datetimeFormater.format(date));
				   return;
			   }
			   if("date".equals(type)){
				   out.println(dateFormater.format(date));
				   return;
			   }
	}
}
